<?php
/*
 * @Author: your name
 * @Date: 2020-05-21 18:04:49
 * @LastEditTime: 2020-05-26 09:56:08
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /v3/application/adminapiv3/service/attackLogManage/fileLogManage/FileIllegalModifyService.php
 */ 
namespace app\adminapiv3\service\attackLogManage\fileLogManage;

class FileIllegalModifyService extends FileLogService
{
    /**
     * @name: 列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-21 18:08:55
     */
    function list ($input)
    {
        $input['host_id']= $this->belongedHost();
        $return = $this->DbList($input);
        $return['data'] = $this->handleData($return['data']);

        return $return;
    }

    /**
     * @name: 数据库列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-21 18:10:01
     */
    function DbList ($filterData)
    {
        $server = db('FileIllegalModify')->alias('fm')
                    ->join('host h', 'fm.host_id = h.id')
                    ->field('fm.*, h.name');


        !isset($filterData['host_id']) || empty($filterData['host_id']) ? : $server->whereIn('fm.host_id', $filterData['host_id']);
        !isset($filterData['attack_dest']) || empty($filterData['attack_dest']) ? : $server->whereLike('h.name', '%'.$filterData['attack_dest'].'%');
        !isset($filterData['type']) || empty($filterData['type']) ? : $server->whereLike('fm.type', '%'. $filterData['type'] .'%');
        if (isset($filterData['atime']) && !empty($filterData['atime']))
        {
            $stime = strtotime($filterData['atime']);
            $etime = $stime + 86400;
            $server->whereBetween('fm.atime', [$stime,$etime]);
        }


        !isset($filterData['dest_ip']) || empty($filterData['dest_ip']) ? : $server->whereLike('fm.dest_ip', '%'. $filterData['dest_ip'] .'%');



        !isset($filterData['process']) || empty($filterData['process']) ? : $server->whereLike('fm.process', '%'.$filterData['process'].'%');
        !isset($filterData['path']) || empty($filterData['path']) ? : $server->whereLike('fm.path', '%'. $filterData['path'] .'%');

        if (isset($filterData['ctime']) && !empty($filterData['ctime']))
        {
            $stime = strtotime($filterData['ctime']);
            $etime = $stime + 86400;
            $server->whereBetween('fm.ctime', [$stime,$etime]);
        }


        if (isset($filterData['handle']) && is_numeric($filterData['handle'])) {
            $server->where('handle', $filterData['handle']);
        }


        $return = $server->order('fm.id', 'desc')->paginate($filterData['limit'])->toArray();


        return $return;
    }

         /**
     * @name: 处理数据
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-09-29 10:58:44
     */
    function handleData ($data)
    {
        $return = [];
        if(empty($data))
        {
            return $return;
        }

        foreach ($data as $k => $v)
        {
            $return[$k] = [
                'id' => $v['id'],
                'attack_dest' => $v['name'],
                'type' => $v['type'],
                'path' => $v['path'],
                'process' => $v['process'],
                'atime' => date('Y-m-d h:i:s', $v['atime']),
                'ctime' => $v['ctime'],
                'handle' => $this->handle2string($v['handle']) ,
            ];
        }

        return $return;
    }

    /**
     * @name: 删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-22 10:57:44
     */
    function delete ($id_s)
    {
        $host_id_a = $this->nowGroupHost();
        db('FileIllegalModify')->whereIn('id', $id_s)->whereIn('host_id', $host_id_a)->delete();
    }

    /**
     * @name: 清空
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-22 10:57:44
     */
    function clear ()
    {
        $host_id_a = $this->nowGroupHost();
        db('FileIllegalModify')->whereIn('host_id', $host_id_a)->delete();
    }
}